package com.surprise.base.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.beanutils.converters.IntegerConverter;
import org.apache.commons.beanutils.converters.LongConverter;
import org.apache.commons.collections.CollectionUtils;

import com.google.common.collect.Lists;

/**
 * StringUtils
 * 
 * @author nick
 */
public class StringUtils {

    /**
     * use single char in delim as delim
     * 
     * @param str
     * @param delim
     * @return
     */
    public static List<String> wiseSplit(String str, char[] delims) {
        return wiseSplit(str, delims, false);
    }

    /**
     * preserveAllTokens when true
     * 
     * @param str
     * @param delims
     * @param preserveAllTokens
     * @return
     */
    public static List<String> wiseSplit(String str, char[] delims, boolean preserveAllTokens) {
        if (str == null) {
            return null;
        }
        List<String> ret = Lists.newLinkedList();
        final int len = str.length();
        if (len == 0) {
            return ret;
        }
        if (delims == null || delims.length == 0) {
            ret.add(str);
            return ret;
        }

        int i = 0, start = 0;
        boolean match = false;
        boolean lastMatch = false;
        while (i < len) {
            if (in(str.charAt(i), delims)) {
                if (match || preserveAllTokens) {
                    ret.add(str.substring(start, i));
                    match = false;
                    lastMatch = true;
                }
                start = ++i;
                continue;
            }
            lastMatch = false;
            match = true;
            i++;
        }
        if (match || preserveAllTokens && lastMatch) {
            ret.add(str.substring(start, i));
        }
        return ret;
    }

    public static boolean in(char c, char[] cs) {
        for (char s : cs) {
            if (c == s) {
                return true;
            }
        }
        return false;
    }

    static {
        // by default, we use strict converter, which means it throws Exception
        // when type do not match.
        BeanUtilsBean.getInstance().getConvertUtils().register(new IntegerConverter(),
                Integer.class);
        BeanUtilsBean.getInstance().getConvertUtils().register(new LongConverter(), Long.class);
    }

    /**
     * split to List<T>, don't use this method when you are not sure what the result may be.
     * 
     * @param <T>
     * @param str
     * @param delim
     * @param clazz List element type
     * @return
     * @exception ConversionException
     */
    public static <T> List<T> split(String str, String delim, Class<T> clazz) {
        if (str == null || delim == null) {
            return null;
        }
        String[] sArray = split(str, delim);
        List<T> list = new ArrayList<T>();
        if (clazz == String.class) {
            for (Object s : sArray) {
                list.add((T) s);
            }
        } else {
            // if you want to convert it by your own way, please register your
            // own converter
            Converter converter = BeanUtilsBean.getInstance().getConvertUtils().lookup(clazz);
            for (Object s : sArray) {
                T o = (T) converter.convert(clazz, s);
                list.add(o);
            }
        }
        return list;
    }

    /**
     * split
     * 
     * @param str
     * @param delim
     * @return
     */
    public static String[] split(String str, String delim) {
        return org.apache.commons.lang.StringUtils.split(str, delim);
    }

    public static <T> String join(Collection<T> c, String delim) {
        if (CollectionUtils.isEmpty(c)) {
            return "";
        }
        return org.apache.commons.lang.StringUtils.join(c, delim);
    }

    public static boolean isEmpty(String str) {
        return org.apache.commons.lang.StringUtils.isEmpty(str);
    }

    public static String safeCutString(String s, int length) {
        return org.apache.commons.lang.StringUtils.substring(s, 0, length);
    }

    public static <T> T safeConvert(String str, Class<T> clazz) {
        Converter converter = BeanUtilsBean.getInstance().getConvertUtils().lookup(clazz);
        return (T) converter.convert(clazz, str);
    }

    public static String replaceByStar(String phone) {
        if (StringUtils.isEmpty(phone)) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        int length = phone.length();
        for (int i = 0; i < length; i++) {
            if (i < 3 || i >= length - 4) {
                sb.append(phone.charAt(i));
            }
            if (i == 3) {
                sb.append("****");
            }
        }
        return sb.toString();
    }

    public static String splitOne(String str, String delim) {
        if (StringUtils.isEmpty(str)) {
            return "";
        }
        String[] ss = split(str, delim);
        return ss[0];
    }
    
    public static String cutTrim(String str, String delim) {
        return str.replaceAll(str, delim).trim();
    }

    public static String nullToSpace(String str) {
    	if (str == null) {
    		return "";
    	}
    	return str;
    }

    public static void main(String[] args) {
        System.out.println(replaceByStar("18721658552"));
    }
}
